<?php
class Subpage
{
    const MATCH_EXTEND              = 'match_extend';
    const COMMENT_LIST              = 'comment_list';
    
    /**
     * Parent instance
     */
    private static $_modelParent    = null;

    /**
     * Instance
     */
    protected static $_instance     = null;

    /**
     * Constructor of class
     */
    protected function  __construct()
    {
        //Init parent model
        $options    = array(
                'adapter'   => 'mysql'
        );
        $this->_modelParent = Fpt_Business_Subpage::getInstance($options);
    }

    /**
     * Get singleton instance
     */
    public final static function getInstance()
    {
        //Check instance
        if ( is_null(self::$_instance) )
        {
            self::$_instance    = new self();
        }//end if

        //Return instance
        return self::$_instance;
    }
    
    /**
     * Insert Subpage
     */
    public function insertSubpage($params)
    {
        $matchId  = $this->_modelParent->insertSubpage($params);

        //Return
        return $matchId;
    }
    
    /**
     * Update Match Extend
     */
    public function updateMatchExtend($params){
        
        $result  = $this->_modelParent->updateMatchExtend($params);
        
        // Detele cache
        if ($result) {
            $keyCache   = Fpt_Global::makeCacheKey(self::MATCH_EXTEND, $params['match_id']);
            Fpt_Global::deleteMemcache($params['match_id'], $keyCache);
        }
        
        return $result;
    }
    
    /**
     * get List Match_Extend
     */
    public function getListMatchExtend($condition,$limit,$offset, $orderby){
        $result = $this->_modelParent->getListMatchExtend($condition,$limit,$offset, $orderby);
        return $result;
    }

    /**
     * Upload file
     */
    public function uploadFile($matchid, $arrimage, $uploaded_file, $static_path, $strPath)
    {        
        //Set return
        $return = array('thumb'=>null);

        if ( !empty($arrimage) )
        {           
            //Set root path
            $thumb = array();            

            $uploaded_file->setDestination($strPath);   
            $details = $this->getDetailMatchExtend($matchid);
            foreach ( $uploaded_file->getFileInfo() as $key => $info )
            {                
                if (!empty($info['name']) && $info['error'] == 0 )
                {                  
                    switch ($key){
                    case 'squad_content': 
                        $thumb['image'] = $this->uploadImage($uploaded_file, $details, $info['name'], $arrimage['image'], 'squad_content', $strPath);
                        break;
                    case 'image_team1': 
                        $thumb['image1'] = $this->uploadImage($uploaded_file, $details, $info['name'], $arrimage['image1'], 'image_team1', $strPath, $i = 1);
                        break;
                    case 'image_team2': 
                        $thumb['image2'] = $this->uploadImage($uploaded_file, $details, $info['name'], $arrimage['image2'], 'image_team2', $strPath, $i = 2);
                        break;
                    case 'image_background':
                        $thumb['image3'] = $this->uploadImage($uploaded_file, $details, $info['name'], $arrimage['image3'], 'image_background', $strPath, $i = 3);
                        break;
                    }
                }//end if
            }//end foreach

        }//end if
        //Return

        return $thumb;
    }
    
    /**
     * upload image
     */
    private function uploadImage($uploaded_file, $details, $imageUpload, $imageName, $fileName, $strPath, $i =''){
        $strPos     = strpos($imageName, '.', strlen($imageName) - 4);
        $strExt     = substr($imageName, $strPos + 1, 3);  

        //Get extension
        switch ($strExt)
        {
            case 'image/jpeg':
                $strExt = 'jpg';
                break;
            case 'image/gif':
                $strExt = 'gif';
                break;
            case 'image/png':
                $strExt = 'png';
                break;
        }//end switch
        $thumb['image'] = '';
        if(!empty ($imageUpload) && $imageUpload == $imageName ){
            $strFilename    = $i . time() . '.' . $strExt;
            //Rename file
            $uploaded_file->addFilter('Rename', array('target'=>$strPath.Fpt_Filter::upperTolower($strFilename), 'overwrite'=>true));
            $thumb['image'] = '/site/'.Fpt_Filter::upperTolower($strFilename);
            if ( !$uploaded_file->receive($imageUpload) )
            {
                if (!empty ($details[$fileName]))
                {
                    //remove image
                    unlink($static_path.$details[$fileName]);
                }//end if
            }
        }
        return $thumb['image'];
    }


    /**
     * get Detail Match_Extend
     */
     public function getDetailMatchExtend($matchId)
    {
        $result = $this->_modelParent->getDetailMatchExtend($matchId); 
        //Return
        return $result;
    }
    
    /**
     * delete Subpage
     */
    public function deleteSubpage($matchid)
    {
        if ( $this->_modelParent->deleteSubpage($matchid) )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    /**
     * get Detail comment
     */
     public function getDetailComment($commentId)
    {
        $result = $this->_modelParent->getDetailComment($commentId); 
        return $result;
    }
    
    /**
     * Get list comment by match ID
     */
    public function getListCommentByMatch($arrParam)
    {       

        $result = $this->_modelParent->getListCommentByMatch($arrParam);
        
        //Set return
        $return = array('list'=>null, 'total'=>0);

        if ( !empty($result) && is_array($result) )
        {
            $result = array_reverse($result);
            $return['total']    = count($result);
            $return['list']     = $result;
        }//end if

        return $return;
    }
    
    /**
     * Get list comment
     */
    public function getListComment($params)
    {
        //Return
        return $this->_modelParent->getListComment($params);
    }
    
    /**
     * delete comment
     */
    public function deleteComment($arrParam)
    {
        if ( $this->_modelParent->deleteComment($arrParam['comment_id']) )
        {
            // Detele cache
            $keyCache   = Fpt_Global::makeCacheKey(self::COMMENT_LIST, array($arrParam['match_id'], $arrParam['team_id']));
            Fpt_Global::deleteMemcache($arrParam['match_id'], $keyCache);
          
             return true;
        }
        else
        {
            return false;
        }//end if
    }
    
    /**
     * update comment
     */
    public function updateComment($arrParam)
    {
        
        if ( $this->_modelParent->updateComment($arrParam) )
        {
            // Detele cache
            $keyCache   = Fpt_Global::makeCacheKey(self::COMMENT_LIST, array($arrParam['match_id'], $arrParam['team_id']));
            Fpt_Global::deleteMemcache($arrParam['match_id'], $keyCache);
          
            //Return
            return true;
        }
        else
        {
            return false;
        }//end if
    }
    
     /**
     * Log error
     */
    public static function logError($e)
    {
        if ( APP_ENV == 'development' )
        {
            echo $e->getMessage();
        }//end if
    }
}

